package cn.niit.pm.controller;

import cn.niit.pm.entity.Attendance;
import cn.niit.pm.entity.LeaveForm;
import cn.niit.pm.entity.Train;
import cn.niit.pm.service.impl.AttendanceServiceImpl;
import cn.niit.pm.service.impl.LeaveFormServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api
@Controller
public class LeaveController {

    @Autowired
    private LeaveFormServiceImpl service;
    /**
     * 请请假管理/假申请登录页面
     * @return
     */
    @GetMapping("/attendance_approval")
    public String leavemanagementIndex(LeaveForm leaveForm,
                                       @RequestParam(value = "pageSize", defaultValue = "0") int pageSize,
                                       @RequestParam(value = "pageNumber", defaultValue = "10") int pageNumber, Model model) {
        PageInfo info = service.select(leaveForm, pageSize, pageNumber);
        model.addAttribute("pageInfo", info);
        return "leave_management/attendance_approval";
    }

    @PostMapping("/attendance_approval/insert")
    @ResponseBody
    public void insert(@RequestParam("employeeId") Long employeeId,
                       @RequestParam("formType") Integer formType,
                       @RequestParam(value = "startTime",required = false) String startTime,
                       @RequestParam(value = "endTime",required = false) String endTime,
                       @RequestParam("reason") String reason,
                       @RequestParam("state") String state
    ) {
        LeaveForm leaveForm = new LeaveForm();
        leaveForm.setEmployeeId(employeeId);
        leaveForm.setFromType(formType);
        leaveForm.setStartTime(new Date());
        leaveForm.setEndTime(new Date());
        leaveForm.setReason(reason);
        leaveForm.setCreateTime(new Date());
        leaveForm.setState(state);
        service.insert(leaveForm);
    }

    @PostMapping("/attendance_approval/update")
    @ResponseBody
    public void save(@RequestParam("formId") Long formId,
                     @RequestParam("employeeId") Long employeeId,
                     @RequestParam("formType") Integer formType,
                     @RequestParam(value = "startTime",required = false) Date startTime,
                     @RequestParam(value = "endTime", required = false) Date endTime,
                     @RequestParam("reason") String reason,
                     @RequestParam("state") String state
    ) {
        LeaveForm leaveForm = new LeaveForm();
        leaveForm.setFromId(formId);
        leaveForm.setEmployeeId(employeeId);
        leaveForm.setFromType(formType);
        leaveForm.setStartTime(new Date());
        leaveForm.setEndTime(new Date());
        leaveForm.setReason(reason);
        leaveForm.setState(state);
        service.update(leaveForm);
    }

    @GetMapping("/attendance_approval/delete/{id}")
    @ResponseBody
    public void delete(@PathVariable("id") Integer id) {
        service.delete(id);
    }

    @GetMapping("/attendance_approval/selectById/{id}")
    @ResponseBody
    public LeaveForm selectById(@PathVariable("id") Long id) {
        return service.selectById(id);
    }

    @GetMapping("/attendance_approval/selectTrainerById")
    @ResponseBody
    public List<Train> selectTrainerById(Model model) {
        List<Train> trains = service.selectTrainerById();
        return trains;
    }
}
